package com.yima.baseproj.module.app.config;

import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.app.config.model.AppConfigEditParamModel;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.ResultVo;
import com.yima.baseproj.module.config.utils.ResultUtil;
import com.yima.baseproj.security.annotation.SecurityIgnoreUrl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "配置管理")
@RequestMapping("/api/app/config")
@RestController
public class AppConfigController extends BaseController {

    @Autowired
    private AppConfigService appConfigService;

    @ApiOperation("查询通过类型")
    @SecurityIgnoreUrl
    @GetMapping("/findByType/{type}")
    public ResultVo getSysConfigList(
            @PathVariable
            @NotBlank(message = "缺少必要字段")
            String type) {
        AppConfigEntity appConfigEntity = appConfigService.findByType(type);
        return ResultUtil.success(appConfigEntity);
    }

    @ApiOperation("修改")
    @PostMapping("/edit")
    public void edit(@RequestBody AppConfigEditParamModel paramModel) {
        AppConfigEntity appConfigEntity = appConfigService.findByType(paramModel.getType());
        if(appConfigEntity == null){
            throw ApiException.getInstance("");
        }
        appConfigEntity.setData(paramModel.getValue());
        appConfigService.saveOrUpdate(appConfigEntity);
    }
}
